package ace.cmp.security.oauth2.resource.server.test.core.controller;

import ace.cmp.core.model.R;
import ace.cmp.json.api.JsonService;
import ace.cmp.security.core.impl.model.dto.AceDefaultIntrospectPrincipalDto;
import ace.cmp.security.core.impl.model.dto.AceIntrospectPrincipalDto;
import ace.cmp.security.oauth2.resource.server.api.TokenAuthenticationApi;
import ace.cmp.security.oauth2.resource.server.core.constant.Oauth2ResourceServerConstant;
import ace.cmp.security.oauth2.resource.server.core.properties.Oauth2ResourceServerSecurityProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * @author caspar
 * @date 2023/2/14 10:58
 */
@Slf4j
@RestController
@AllArgsConstructor
public class OAuth2TokenTestController implements TokenAuthenticationApi {

  public static final String TOKEN_INTROSPECT_URI = "/test/oauth2/token/introspect";
  private final JsonService jsonService;
  private final Oauth2ResourceServerSecurityProperties properties;

  @PostMapping(TOKEN_INTROSPECT_URI)
  @Override
  public R<AceIntrospectPrincipalDto> introspect(String authorization, String token) {
    Assert.hasText(authorization);
    Assert.hasText(token);
    log.info("token: " + token);
    if (token.equalsIgnoreCase(Oauth2ResourceServerConstant.SCOPE_USERINFO)) {
      log.info(properties.getInMemoryIntrospectPointUserInfoJson());
      AceIntrospectPrincipalDto result =
          jsonService.toObject(
              properties.getInMemoryIntrospectPointUserInfoJson(),
              AceDefaultIntrospectPrincipalDto.class);
      return R.ok(result);
    }
    if (token.equalsIgnoreCase(Oauth2ResourceServerConstant.SCOPE_INTERNAL_CLIENT)) {
      log.info(properties.getInMemoryIntrospectPointTokenJson());
      AceIntrospectPrincipalDto result =
          jsonService.toObject(
              properties.getInMemoryIntrospectPointTokenJson(),
              AceDefaultIntrospectPrincipalDto.class);
      return R.ok(result);
    }
    return R.fail(null);
  }

  @ResponseBody
  @PostMapping("/oauth2/introspect")
  public Object introspect2(@RequestParam("token") String token) {

    log.info("token: " + token);
    String demoJon =
        "{\"active\":true,\"sub\":\"accountId\",\"aud\":[\"defaultClientId\"],\"nbf\":1698386698,\"scope\":\"all"
            + " openid"
            + " profile\",\"iss\":\"http://localhost:8080\",\"attributes\":{\"nickName\":\"nickName\"},\"exp\":1698388498,\"iat\":1698386698,\"jti\":\"fcc0e07b-52b8-4db1-9329-3bb439edc1b6\",\"client_id\":\"defaultClientId\",\"token_type\":\"Bearer\"}";

    Assert.hasText(token);
    log.info("token: " + token);
    if (token.equalsIgnoreCase(Oauth2ResourceServerConstant.SCOPE_USERINFO)) {
      log.info(properties.getInMemoryIntrospectPointOriginTokenUserInfoJson());

      return jsonService.toMap(
          properties.getInMemoryIntrospectPointOriginTokenUserInfoJson(),
          String.class,
          Object.class);
    }
    if (token.equalsIgnoreCase(Oauth2ResourceServerConstant.SCOPE_INTERNAL_CLIENT)) {
      log.info(properties.getInMemoryIntrospectPointOriginTokenJson());
      return jsonService.toMap(
          properties.getInMemoryIntrospectPointOriginTokenJson(), String.class, Object.class);
    }
    return R.fail(null);
  }
}
